package com.simple.portal.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.portal.bo.IApplicationBO;
import com.simple.portal.bo.IOrganizationBO;
import com.simple.portal.vo.ApplicationVO;
import com.simple.portal.vo.OrganizationVO;
import com.simple.util.TbResourceLoad;
import com.simple.util.common.Constants;
import com.simple.util.common.RightsConstants;
import com.simple.util.common.SpringUtil;
import com.simple.util.object.PageInfoBean;
import com.simple.util.object.portal.ApplicationBean;
import com.simple.util.object.portal.SessionUserBean;

import freemarker.template.TemplateException;

@Controller
@RequestMapping("/app")
public class AppController extends AbstractPortalBaseController{
	private final static String JSP_PATH = "_App";
	
	@Autowired
	private IApplicationBO applicationBO;
	
	@Autowired
	private IOrganizationBO organizationBO;
		
	@RequestMapping("/list")  
    public String appList(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    		 		   @RequestParam(value="applicationNameSearch",required=false) String applicationNameSearch,
    		 		   @RequestParam(value="providerNameSearch",required=false) String providerNameSearch,
    		 		   final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_APP_LIST);
		if(rightsFlag){
			
		        final PageInfoBean pageInfoBean = new PageInfoBean(pageNo);
		        SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
		        
		        List<ApplicationBean> applicationBeanList = applicationBO.findApplicationByPage(sessionUserBean.getUserInfoVO().getUserId(), applicationNameSearch, providerNameSearch, pageInfoBean);
	        
		        List<OrganizationVO> findProviderList = organizationBO.findProviderList(sessionUserBean.getUserInfoVO().getUserId(),"P");        
		        
				Map<String, String> statusMap= TbResourceLoad.getInstance().getMap(Constants.TBRESOURCES_PACKAGE_MAP_STATUS, getSiteLanguage(request));
				
		        request.setAttribute("applicationBeanList", applicationBeanList);
		        request.setAttribute("findProviderList", findProviderList);
		        request.setAttribute("statusMap", statusMap);
				 
				final String pageInfo = super.generatePageInfo(request,response,pageInfoBean, "goPage");
			    request.setAttribute("pageInfo", pageInfo);  
			    
			    request.setAttribute("pageNo", pageNo); 
			    request.setAttribute("applicationNameSearch", applicationNameSearch);  
			    request.setAttribute("providerNameSearch", providerNameSearch);  
			    request.setAttribute("statusOne", Constants.STATE_NORMAL);
			    request.setAttribute("statusZero", Constants.STATE_STOP);
			    
			    request.setAttribute("recordCountInOnePage", PageInfoBean.COMMON_RECORD_COUNT_IN_ONE_PAGE);
			    request.setAttribute("siteLanguage", getSiteLanguage(request));
		}
			        
        return getJspPath("app_list");
    }
	
	@RequestMapping("/add")  
    public String appAdd(@ModelAttribute("applicationVO") ApplicationVO applicationVO,
    					final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_APP_ADD);
		boolean flag =  checkIsAppCreator(applicationVO.getApplicationId(), request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			Map<String, String> applicationTypeMap= TbResourceLoad.getInstance().getMap(Constants.TBRESOURCES_PACKAGE_MAP_APPLICATION_TYPE, getSiteLanguage(request));
			List<OrganizationVO> findProviderList	= organizationBO.findProviderList(sessionUserBean.getUserInfoVO().getUserId(),"P");
			 
			request.setAttribute("applicationTypeMap", applicationTypeMap);
			request.setAttribute("findProviderList", findProviderList);
		}
		
		return getJspPath("app_add");
    }
	
	@RequestMapping(value="/addSubmit",method=RequestMethod.POST)  
    public String appAddSubmit(@ModelAttribute("applicationVO") ApplicationVO applicationVO,
    						final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_APP_ADD);
		boolean flag =  checkIsAppCreator(applicationVO.getApplicationId(), request,response);	
		if(rightsFlag && flag){
				SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
				applicationBO.addApplication(applicationVO,sessionUserBean);
		}
        return  "forward:/app/list" ;
    }
	@RequestMapping("/edit")  
    public String appEdit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
			 @RequestParam(value="applicationId",required=false) String applicationId,
			 @RequestParam(value="applicationNameSearch",required=false) String applicationNameSearch,
			 @RequestParam(value="providerNameSearch",required=false) String providerNameSearch,
			 @RequestParam(value="supplier",required=false) String supplier,
			 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  { 
		     
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_APP_EDIT);
		boolean flag =  checkIsAppCreator(applicationId, request,response);
		if(rightsFlag && flag){
			List<OrganizationVO> findProvider = organizationBO.findProvider(applicationId);   
			Map<String, String> applicationTypeMap= TbResourceLoad.getInstance().getMap(Constants.TBRESOURCES_PACKAGE_MAP_APPLICATION_TYPE, getSiteLanguage(request));
			
			request.setAttribute("applicationTypeMap", applicationTypeMap);
			request.setAttribute("applicationVO", applicationBO.findById(applicationId)); 
			request.setAttribute("findProvider", findProvider);
			request.setAttribute("statusYes", Constants.STATE_YES);
		    request.setAttribute("statusNo", Constants.STATE_NO);
		    request.setAttribute("statusOne", Constants.STATE_NORMAL);
		    request.setAttribute("statusZero", Constants.STATE_STOP);
					
			request.setAttribute("pageNo", pageNo); 
			request.setAttribute("applicationNameSearch", applicationNameSearch);  
			request.setAttribute("providerNameSearch", providerNameSearch);  
			
		}
        return getJspPath("app_edit");
    }
	

	@RequestMapping(value="/editSubmit",method=RequestMethod.POST)  
    public String appEditSubmit(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    						 @ModelAttribute("applicationVO") ApplicationVO applicationVO,
    						 @RequestParam(value="applicationNameSearch",required=false) String applicationNameSearch,
    	    				 @RequestParam(value="providerNameSearch",required=false) String providerNameSearch,
							 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_APP_EDIT);
		boolean flag =  checkIsAppCreator(applicationVO.getApplicationId(), request,response);
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			applicationBO.editApplication(applicationVO, sessionUserBean);
		}
        
        return "forward:/app/list";
    }
	@RequestMapping("/control")  
    public String appControl(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
    							@RequestParam(value="applicationId",required=false) String applicationId,
    							@RequestParam(value="status",required=false) String status,
								@RequestParam(value="applicationNameSearch",required=false) String applicationNameSearch,
								@RequestParam(value="providerNameSearch",required=false) String providerNameSearch,
								final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
		boolean rightsFlag= checkRights( request,response,RightsConstants.FUNC_APP_CONTROL);
		boolean flag =  checkIsAppCreator(applicationId, request,response);	
		if(rightsFlag && flag){
			SessionUserBean sessionUserBean = getCurrentSessionUserBean(request);
			
			applicationBO.controlApplication(applicationId, status, sessionUserBean);
		}
	    
	    return "forward:/app/list";
       
    }
	@RequestMapping("/info")  
    public String appInfo(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
			 @RequestParam(value="applicationId",required=false) String applicationId,
			 @RequestParam(value="applicationNameSearch",required=false) String applicationNameSearch,
			 @RequestParam(value="providerNameSearch",required=false) String providerNameSearch,
			 final HttpServletRequest request, final HttpServletResponse response) throws IOException, TemplateException  {
			
		boolean rightsFlag = checkRights( request,response,RightsConstants.FUNC_APP_EDIT);
		if(rightsFlag){			
			List<OrganizationVO> findProvider = organizationBO.findProvider(applicationId); 
			request.setAttribute("findProvider", findProvider);
			request.setAttribute("applicationVO", applicationBO.findById(applicationId)); 		
		}
        return getJspPath("app_info");
    }
	@Override
    protected String getBaseJspPath() {
        return JSP_PATH;
    }

	private boolean checkIsAppCreator(final String applicationId,final HttpServletRequest request,final HttpServletResponse response) throws IOException {
		boolean flag = false;
		SessionUserBean sessionUserBean =getCurrentSessionUserBean(request);
	    final IApplicationBO applicationBO = (IApplicationBO) SpringUtil.getBean("applicationBO");
	    ApplicationVO applicationVO  = applicationBO.findById(applicationId);
	    if(sessionUserBean.getUserInfoVO().getUserId().equals(applicationVO.getCreator())){
	    	flag = true;
	    }
	 	if(!flag){
		   	final String goUrl = "/message/alert?errorId=message.user.have.no.rights&goPage="+Constants.SYSTEM_ALERT_GOPAGE_HOME;
		    response.sendRedirect(goUrl);
		}
		   	return flag;
   }

}

